package com.kili.picture.tools;

import com.google.common.cache.LoadingCache;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;

public class LocalCacheHolder {

    private static ConcurrentMap<String, LoadingCache<String, Object>> cacheTable = new ConcurrentHashMap<String, LoadingCache<String, Object>>();

    @SuppressWarnings("unchecked")
    public static <T> T getValue(String cacheKey, String itemkey, Class<?> T) {

        try {
            return (T) LocalCacheHolder.getCaches(cacheKey).get(itemkey);
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void setCache(String cacheKey, LoadingCache<String, Object> cache) {

        LocalCacheHolder.cacheTable.put(cacheKey, cache);
    }

    public static LoadingCache<String, Object> getCaches(String cacheKey) {
        return cacheTable.get(cacheKey);
    }

    public static ConcurrentMap<String, LoadingCache<String, Object>> getCacheTable() {
        return cacheTable;
    }

    public static void setCacheTable(ConcurrentMap<String, LoadingCache<String, Object>> cacheTable) {
        LocalCacheHolder.cacheTable = cacheTable;
    }
}